package com.quark.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quark.model.system.user.UserPageVO;
import com.quark.model.system.user.vo.AuUserVO;
import com.quark.model.system.user.vo.UserPasswordVO;
import com.quark.result.RequestResult;
import com.quark.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author 张高昌
 * @date 2021/11/1 20:49
 * @description TODO
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @ApiOperation("退出")
    @GetMapping("logout")
    public RequestResult getInfo() {

        return RequestResult.success(null);
    }


    /**
     * @param model
     * @return
     * @author 张高昌
     * @date 2021/11/1 20:50
     */
    @ApiOperation("用户列表分页查询")
    @GetMapping("/userPageList")
    public RequestResult userPageList(Page page, @Valid UserPageVO model) {
        return RequestResult.success(userService.getMenuPageList(page, model));
    }

    /**
     * @param vo
     * @return
     * @author 张高昌
     * @date 2021/11/1 20:51
     */
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sys_user_add')")
    public RequestResult save(@Valid AuUserVO vo) {

        return RequestResult.success(userService.addUser(vo));
    }

    /**
     * @param id 用户id
     * @return
     * @author 张高昌
     * @date 2021/11/1 21:10
     */
    @ApiOperation(value = "通过id删除用户", notes = "通过id删除用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sys_user_delete')")
    public RequestResult deleteById(@PathVariable Long id) {
        return RequestResult.success(userService.deleteUserById(id));
    }

    @PostMapping("/updateUserStatus/{id}/{status}")
    public RequestResult updateUserStatus(@PathVariable("id") Long id, @PathVariable("status") int status) {
        return RequestResult.success(userService.updateUserState(id, status));
    }

    @ApiOperation(value = "修改用户", notes = "修改用户")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sys_user_update')")
    public RequestResult userUpdate(@Valid AuUserVO vo) {
        return RequestResult.success(userService.updateUser(vo));
    }

    @ApiOperation(value = "修改用户密码", notes = "修改用户密码")
    @PostMapping("updatePassword")
    public RequestResult updatePassword(@Valid UserPasswordVO vo) {

        return RequestResult.success(userService.updateUserPassword(vo));
    }

}
